ESPHome trigger reconnect immediately when mDNS record received#48129
ESPHome trigger reconnect immediately when mDNS record received#48129bdraco merged 7 commits intohome-assistant:devfrom
Conversation
|
Can we reload the config entry instead when it gets rediscovered via zeroconf? #47683 now passes mdns updates and triggers zeroconf discovery |
|
Hi, Unfortunately, that does not work already and would not work. The problem is that HA internally uses the zeroconf Also, it doesn't work already because the #34753 PR only updates the config entry data to set the host, but otherwise does not inform the reconnect logic. If you're worried about resource usage with ZC, this is not an issue because the |
Makes sense. Thanks for the through explanation. |
bdraco
left a comment
There was a problem hiding this comment.
LGTM. Two small suggestions above.
|
Ok, updated per suggestions. 👍 |
|
Looks good. Will do some manual testing later today |
|
Lots of plugging and unplugging and it reconnects just about right after it gets an ip address 👍 This looks good |
| if self._wait_task is not None: | ||
| return | ||
|
|
||
| self._wait_task = self._hass.loop.create_task( |
There was a problem hiding this comment.
We can use asyncio.create_task instead.
| while True: | ||
| try: | ||
| await self._reconnect_once() | ||
| except asyncio.CancelledError: # pylint: disable=try-except-raise |
There was a problem hiding this comment.
From Python 3.8 CancelledError inherits BaseException instead of Exception. So we don't need this case.
There was a problem hiding this comment.
This is not to catch CancelledExceptions, but rather so that if the reconnect task is cancelled (meaning the reconnect loop should stop) we will actually cancel the loop, not just log the error and continue forever.
There was a problem hiding this comment.
Yes, except Exception: won't catch asyncio.CancelledError.
There was a problem hiding this comment.
Ah sorry, yes now I understand.
Thanks, didn't know that! Will fix this later.
| """Start the reconnecting logic background task.""" | ||
| # Create reconnection loop outside of HA's tracked tasks in order | ||
| # not to delay startup. | ||
| self._loop_task = self._hass.loop.create_task(self._reconnect_loop()) |
There was a problem hiding this comment.
Use asyncio.create_task.
Proposed change
Problem: ESPHome's native API has been difficult to use with the deep sleep feature, because home assistant only checks every 60s if the device is online again after sleep, so the device has to stay awake longer until HA connects and wasting power.
Idea: I had an idea yesterday: actually HA already knows when a device comes up again, because on startup the device broadcasts mDNS records on the network.
This PR: So this change implements an mDNS/zeroconf listener that triggers the reconnection logic immediately when such a DNS record is received.
In the process, I also refactored the logic into a new class
ReconnectLogicbecause the code would have gotten messy otherwise. Note: not happy with the tries/connected/reconnect_event variables, but could not find more suitable synchronization primitivesType of change
Additional information
Checklist
black --fast homeassistant tests)If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: